﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web.Configuration;

namespace CRMAdapter.AppCode
{
    public static class VedaIDCheck
    {
        public static string CheckCustomerCredit()
        {
            string s = "";
            StringBuilder xml = new StringBuilder();
            xml.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            xml.Append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">");
            xml.Append("<soapenv:Header xmlns:wsa=\"http://www.w3.org/2005/08/addressing\" xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecuritysecext-1.0.xsd\">");
            xml.Append("<wsse:Security>");
            xml.Append("<wsse:UsernameToken>");
            xml.Append("<wsse:Username>NoBfUn2T8x5J</wsse:Username>");
            xml.Append("<wsse:Password>nObFuN7b3G8k</wsse:Password>");
            xml.Append("</wsse:UsernameToken>");
            xml.Append("</wsse:Security>");
            xml.Append("<wsa:To>http://vedaxml.com/sys2/ev-veda-v1</wsa:To>");
            xml.Append("<wsa:Action>http://vedaxml.com/veda/VerifyIdentityCredit</wsa:Action>");
            xml.Append("<wsa:MessageID>urn:example.com:123456789</wsa:MessageID>");
            xml.Append("</soapenv:Header>");
            xml.Append("<soapenv:Body>");
            xml.Append("</soapenv:Body>");
            xml.Append("</soapenv:Envelope>");
            return s = VedaWebRequest(xml.ToString());

            ////XML of check customer credit
            //string s = "";
            //StringBuilder xml = new StringBuilder();
            //xml.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            //xml.Append("<BCAmessage type=\"REQUEST\">");
            //xml.Append("<BCAaccess>");
            //xml.Append("<BCAaccess-code>" + WebConfigurationManager.AppSettings["BCAaccess-code"].ToString() + "</BCAaccess-code>");
            //xml.Append("<BCAaccess-pwd>" + WebConfigurationManager.AppSettings["BCAaccess-pwd"].ToString() + "</BCAaccess-pwd>");
            //xml.Append("</BCAaccess>");
            //xml.Append("<BCAservice>");
            //xml.Append("<BCAservice-client-ref>0125370857</BCAservice-client-ref>");
            //xml.Append("<BCAservice-code>VDA001</BCAservice-code>");
            //xml.Append("<BCAservice-code-version>V00</BCAservice-code-version>");
            //xml.Append("<BCAservice-data>");
            //xml.Append("<request version=\"1.0\" transaction-reference=\"120506135456\" mode=\"test\">");
            //xml.Append("<subscriber-details>");
            //xml.Append("<subscriber-identifier>" + WebConfigurationManager.AppSettings["subscriber-identifier"].ToString() + "</subscriber-identifier>");
            //xml.Append("<security>" + WebConfigurationManager.AppSettings["security"].ToString() + "</security>");
            //xml.Append("</subscriber-details>");
            //xml.Append("<product name=\"vedascore-financial-consumer-plus-commercial-1.1\" summary=\"yes\"></product>");
            //xml.Append("<individual role=\"principal\">");
            //xml.Append("<individual-name>");
            //xml.AppendFormat("<family-name>{0}</family-name>", VedaCustomerInfo1.FamilyName);
            //xml.AppendFormat("<first-given-name>{0}</first-given-name>", VedaCustomerInfo1.FirstGivenName);
            //xml.Append("</individual-name>");
            //xml.Append("<address type=\"residential-current\">");
            //xml.AppendFormat("<street-number>{0}</street-number>", VedaCustomerInfo1.StreetNumber);
            //xml.AppendFormat("<street-name>{0}</street-name>", VedaCustomerInfo1.StreetName);
            //xml.AppendFormat("<suburb>{0}</suburb>", VedaCustomerInfo1.Suburb);
            //xml.AppendFormat("<state>{0}</state>", VedaCustomerInfo1.State);
            //xml.AppendFormat("<postcode>{0}</postcode>", VedaCustomerInfo1.Postcode);
            //xml.Append("</address>");
            //xml.AppendFormat("<drivers-licence-number country-code=\"AU\">{0}</drivers-licence-number>", VedaCustomerInfo1.DriversLicenceNumber);
            //xml.Append("<gender type=\"female\"></gender>");
            //xml.AppendFormat("<date-of-birth>{0}</date-of-birth>", VedaCustomerInfo1.DateofBirth);
            //xml.Append("</individual>");
            //xml.Append("<enquiry type=\"credit-application\">");
            //xml.Append("<account-type code=\"LC\"></account-type>");
            //xml.AppendFormat("<enquiry-amount currency-code=\"AUD\">{0}</enquiry-amount>", VedaCustomerInfo1.EnquiryAmountCurrencyCode);
            //xml.Append("</enquiry>");
            //xml.Append("</request>");
            //xml.Append("</BCAservice-data>");
            //xml.Append("</BCAservice>");
            //xml.Append("</BCAmessage>");
            //return s = VedaWebRequest(xml.ToString());


            ////XML of testing of VadeWebService
            //StringBuilder xml = new StringBuilder();
            //xml.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            //xml.Append("<BCAmessage type=\"REQUEST\">");
            //xml.Append("<BCAaccess>");
            //xml.Append("<BCAaccess-code>" + WebConfigurationManager.AppSettings["BCAaccess-code"].ToString() + "</BCAaccess-code>");
            //xml.Append("<BCAaccess-pwd>" + WebConfigurationManager.AppSettings["BCAaccess-pwd"].ToString() + "</BCAaccess-pwd>");
            //xml.Append("</BCAaccess>");
            //xml.Append("<BCAservice>");
            //xml.Append("<BCAservice-client-ref>A</BCAservice-client-ref>");
            //xml.Append("<BCAservice-code>PING</BCAservice-code>");
            //xml.Append("<BCAservice-code-version>V00</BCAservice-code-version>");
            //xml.Append("<BCAservice-data>");
            //xml.Append("<request mode='test'>");
            //xml.Append("</request>");
            //xml.Append("</BCAservice-data>");
            //xml.Append("</BCAservice>");
            //xml.Append("</BCAmessage>");
            //VedaWebRequest(xml.ToString());
        }

        public static string VedaWebRequest(string xml)
        {
            string responseFromServer = string.Empty;
            try
            {
                string ServiceURL = "https://ctaau.vedaxml.com/cta/sys2/ev-veda-v1";
                WebRequest request = WebRequest.Create(ServiceURL);
                request.Method = "POST";
                string postData = xml.ToString();
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                request.ContentType = "text/xml";
                request.ContentLength = byteArray.Length;
                Stream dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();
                using (WebResponse response = request.GetResponse())
                {
                    using (StreamReader rd = new StreamReader(response.GetResponseStream()))
                    {
                        responseFromServer = rd.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return responseFromServer;
        }
    }
}
